// pages/livePlayer/liveBrief/index.js
const app = getApp();
let config = require("../../../utils/config");
import { http } from "../../../utils/request";
const WxParse = require("../../../components/wxParse/wxParse");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPage: false,
    userRule: false,

    livePo: null,
    liveInfo: null,
    backPlayCode: null,
    SumCount: 0, //热度

    inviteCode: "",
    inviteAppletId: "",

    playUrl: "",//播放URL
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // options.scene = "VO202403290141";
    this.setData({
      playUrl: options.pull,
      livePo: options.scene ? options.scene : options.po,
      inviteCode: options.invite ? options.invite : "",
      inviteAppletId: options.inviteId ? options.inviteId : ""
    });
  },

  /** 回放密码输入 */
  backPlayCodeInput(e) {
    // e.currentTarget.dataset.item
    this.setData({
      backPlayCode: e.detail.value
    })
  },

  /** 普通客户，需要输入密码才能进入 */
  confirmLiveTap() {
    if (!this.data.backPlayCode) {
      return http.showToast("请输入回放密码");
    }
    if (this.data.backPlayCode != this.data.liveInfo.Pwd) {
      return http.showToast("回放密码错误");
    }

    if (this.data.liveInfo.DisplayModel == 10) { //竖屏直播间
      http.href("/pages/livePlayer/index?po=" + this.data.livePo + "&invite=" + this.data.inviteCode + "&state=30&pull=" + this.data.liveInfo.OnPullUrl);
    } else {//横屏直播间
      http.href("/pages/livePlayer_H/index?po=" + this.data.livePo + "&invite=" + this.data.inviteCode + "&state=30&pull=" + this.data.liveInfo.OnPullUrl);
    }

    // this.queryInviteHistory();
  },

  //店长直接进入直播间
  incomeLiveTap() {
    // http.href("/pages/demo/index");
    // return;
    if (this.data.liveInfo.DisplayModel == 10) { //竖屏直播间
      http.href("/pages/livePlayer/index?po=" + this.data.livePo + "&invite=" + this.data.inviteCode + "&state=30&pull=" + this.data.liveInfo.OnPullUrl);
    } else {//横屏直播间
      http.href("/pages/livePlayer_H/index?po=" + this.data.livePo + "&invite=" + this.data.inviteCode + "&state=30&pull=" + this.data.liveInfo.OnPullUrl);
    }
  },

  /** 查询邀请记录 */
  queryInviteHistory() {
    let that = this
    http.requestData("GET", "Live/CheckRoom", {
      po: this.data.livePo
    }, true).then(res => {
      if (res.Tag == 10) {
        if (that.data.liveInfo.DisplayModel == 10) { //竖屏直播间
          http.href("/pages/livePlayer/index?po=" + that.data.livePo + "&invite=" + res.Data + "&state=30&pull=" + that.data.liveInfo.OnPullUrl);
        } else {//横屏直播间
          http.href("/pages/livePlayer_H/index?po=" + that.data.livePo + "&invite=" + res.Data + "&state=30&pull=" + that.data.liveInfo.OnPullUrl);
        }
      } else {
        http.showToast(res.Message);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //首次登录 无缓存时
    let that = this;
    if (app.globalData.firstShow) {
      app.globalData.firstShow = false;
      app.initCallBack = res => {
        if (!config.OPENID() || !config.TEL()) {
          return http.href("/pages/authlogin/index?from=brief");
        }
        if (config.ROLE()) { //店长
          that.queryLiveInfo();
        } else {
          that.saveInvite(); //非店长，先保存邀约记录
        }
      };
      return;
    }

    //通过界面跳转 或者 已有登录缓存信息
    if (!config.OPENID() || !config.TEL()) {
      return http.href("/pages/authlogin/index?from=brief");
    }

    setTimeout(() => {
      if (config.ROLE()) { //店长
        that.queryLiveInfo();
      } else {
        that.saveInvite(); //非店长，先保存邀约记录
      }
    }, 500);
  },

  /** 直播介绍 */
  queryLiveInfo() {
    let that = this;
    http.requestData("GET", "Live/GetRoomData", {
      PO: this.data.livePo
    }, true).then(res => {
      if (res.Tag == 10) {
        if (res.Data.Cover) {
          res.Data.Cover = config.hostUrl + res.Data.Cover;
        }
        that.setData({
          liveInfo: res.Data,
          userRule: config.ROLE(),
          showPage: res.Data.Status == 30 ? true : false,
          SumCount: res.Extend.SumCount
        })
        wx.hideLoading();
        if (res.Data.Status != 30) { //非结束状态，直接进入直播间
          if (res.Data.DisplayModel == 10) { //竖屏直播间
            http.href("/pages/livePlayer/index?po=" + res.Data.PO + "&invite=" + that.data.inviteCode + "&state=30&pull=" + res.Data.OnPullUrl, true);
          } else {//横屏直播间
            http.href("/pages/livePlayer_H/index?po=" + res.Data.PO + "&invite=" + that.data.inviteCode + "&state=30&pull=" + res.Data.OnPullUrl, true);
          }
          return;
        }
        if (res.Data.Introduce) {
          WxParse.wxParse('live', 'html', res.Data.Introduce, that);
        }
      } else {
        http.showToast(res.Message);
      }
    })

  },

  /** 邀约人记录保存 */
  saveInvite() {
    let that = this;
    http.requestData("POST", "InviteHistory/SaveHistory", {
      PO: this.data.livePo,
      inviteCode: this.data.inviteCode,
      AppletId: this.data.inviteAppletId
    }).then(res => {
      if (res.Tag == 10) {
        that.queryLiveInfo();
      } else {
        http.showToast(res.Message)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {
  // }
})